<?php

declare(strict_types=1);

namespace Upside\Std;

/**
 * @property numeric[] $items
 */
class NumArr extends Arr
{
    public function sum(): int|float
    {
        return \array_sum($this->items);
    }

    public function avg(): int|float
    {
        return $this->sum() / $this->count();
    }

    public function min(): int|float
    {
        if ($this->is_empty()) {
            throw new \LogicException('Array is empty');
        }

        return \min($this->items);
    }

    public function max(): int|float
    {
        if ($this->is_empty()) {
            throw new \LogicException('Array is empty');
        }

        return \max($this->items);
    }

    public function median(): int|float
    {
        if ($this->is_empty()) {
            throw new \LogicException('Array is empty');
        }

        $count = $this->count();
        $index = \floor($count / 2);

        if ($count & 1) { // Если не чётное побитовое сравнение
            return $this->items[$index];
        }

        return ($this->items[$index - 1] + $this->items[$index + 1]) / 2;
    }
}
